package leaderus.study;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;

import com.google.common.collect.ArrayTable;

public class GenRandomersonDBFile {
	public static String path = "d:/leaderus/data";
	public static String fileName="persondb.txt";
	public static void main(String[] args) {
		GenRandomersonDBFile genDB=new GenRandomersonDBFile();
		genDB.generateFile();
	}
	
	public void generateFile(){
			ArrayTable<Integer, char[], Integer> arrayTable;
			String randomId;
			Integer age;
			String name;
			StringBuilder info=new StringBuilder();
			
			File f = new File(path);
			File file = new File(path+"//"+fileName);
			PrintWriter pw;
			try {
				if (!f.exists()) {
					f.mkdirs();
				}
				if (!file.exists()) {
					file.createNewFile();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				
			}
			try {
				System.out.println("正在生成统计文件。。。");
				pw=new PrintWriter(file);
				for (int i = 0; i < 1000000; i++) {
					randomId=getRandomId();
					age = this.getRandomAge(1, 18);
					name = this.getRandomName(8);
					pw.println(info.append(randomId).append(",").append(name).append(",").append(age));
					info.delete(0, info.length());
				}
				pw.flush();
				pw.close();
				System.out.println("统计文件生成完成。。。");
			} catch (FileNotFoundException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
	}
	
	public String getRandomId(){
		StringBuilder str=new StringBuilder();
		str.append(new Random().nextInt(9)+1).append(String.valueOf(System.nanoTime()).substring(6, 14));
		return str.toString();
	}
	public Integer getRandomAge(int min,int max){
		Random randomAge=new Random();
		return (randomAge.nextInt(max)+min);
	}

	/**
	 * java生成随机数字和字母组合
	 * 
	 *
	 */
	public String getRandomName(int length) {
		StringBuilder name=new StringBuilder();
		Random random = new Random();
		for (int i = 0; i < length; i++) {
			// 输出字母还是数字
			String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
			// 字符串
			if ("char".equalsIgnoreCase(charOrNum)) {
				// 取得大写字母还是小写字母
				int choice = random.nextInt(2) % 2 == 0 ? 65 : 97;
				name.append((char)(choice + random.nextInt(26)));
			} else if ("num".equalsIgnoreCase(charOrNum)) {
				// 数字
				name.append(random.nextInt(10));
			}
		}
		return name.toString();
	}

}
